﻿#ifndef ROC_SERIAL_H
#define ROC_SERIAL_H


#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <termios.h>
#include <string.h>
#include <iostream>
// File control definitions
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>


/*!  \class     Serial
\brief     This class is used for communication over a serial lidar_device_.
*/
class Serial
{
public:
    Serial();

    // Destructor
    ~Serial();

    int64_t GetTimeStamp();
    // Open a lidar_device_
    char openDevice (const char *Device, unsigned int Bauds);

    // Close the current lidar_device_
    void closeDevice();

    char writeChar(const char Byte);

    int readChar(unsigned char *pByte,const unsigned int TimeOut_ms=0);

    int readChars( unsigned char *Buffer, unsigned int MaxNbBytes, const unsigned int TimeOut_ms = 0);
    int readData( unsigned char *Buffer, unsigned int expectedBytes);

    // Empty the received buffer
    void flushReceiver();

    // Return the number of bytes in the received buffer
    int peekReceiver();

    // Change CTR Status
    void DTR(bool Status);

    // Change RTS Status
    void RTS(bool Status);

    // Get CTS bit
    bool isCTS();

    // Get DTR bit
    bool isDTR();

    // Get CTS bit
    bool isRTS();
private:
    bool mHandlesOpen_;

#if defined (_WIN32) || defined( _WIN64)
    HANDLE hSerial;
    COMMTIMEOUTS timeouts;
#endif
#ifdef __linux__
    int fd;
#endif

};



#endif // ROC_SERIAL_H
